<template>
  <div class="box">
    <!-- 搜索输入框及按钮 -->
    <div class="BoxTop">
      <el-input placeholder="请输入对象KEY" v-model="key" clearable> </el-input>
      <el-input placeholder="请输入对象名称" v-model="name" clearable> </el-input>
      <el-button type="primary" class="el-icon-search" @click="handleAll">查找</el-button>
      <el-button type="primary" class="el-icon-edit" @click="handleKai">添加</el-button>
      <el-button type="primary" class="el-icon-download" @click="downLoad"
        >导出</el-button
      >
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <el-upload
        class="avatar-uploader"
        action="http://admin.bsmall.byesame.com/admin/storage/create"
        :headers="headers"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
      >
        <el-button type="primary" v-if="title == '上传对象'">点击上传</el-button>
      </el-upload>
      <el-form
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        v-if="title == '修改对象名称'"
      >
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="bianrow.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 表格 -->
    <div class="BoxMiddle">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="key" label="对象KEY" width="180" align="center">
        </el-table-column>
        <el-table-column prop="name" label="对象名称" width="180" align="center">
        </el-table-column>
        <el-table-column prop="type" label="对象类型" align="center"> </el-table-column>
        <el-table-column prop="size" label="对象大小" align="center"> </el-table-column>
        <el-table-column prop="url" label="图片" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.url" alt="" style="width: 50px; height: 50px" />
          </template>
        </el-table-column>
        <el-table-column prop="url" label="图片链接" align="center"> </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleKaibian(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="BoxBottom">
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="obj.currentPage"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="obj.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="obj.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getOs, postDelete, postUpdate } from "@/api/xitongguanli/os.js";
import * as XLSX from "xlsx"; // 批量导出
export default {
  data() {
    return {
      // 输入框数据
      key: "",
      name: "",
      // 表格数据
      tableData: [],
      // 分页
      obj: {
        page: 1,
        limit: 20,
        total: null,
        currentPage: 1,
      },
      // 模态框
      dialogVisible: false,
      title: "",
      // 暂存图片
      imageUrl: "",
      // 请求头
      headers: {
        "x-litemall-admin-token": "e11a9bc5-67f5-4b1b-8e61-ee2646498a58",
      },
      // 编辑的本行数据
      bianrow: {},
      // 编辑模态框验证
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 接口总数据和搜索
    handleAll() {
      getOs({
        page: this.obj.page,
        limit: this.obj.limit,
        key: this.key,
        name: this.name,
        sort: "add_time",
        order: "desc",
      }).then((res) => {
        this.tableData = res.data.list;
        this.obj.total = res.data.total;
        console.log(res.data, "222222");
      });
    },
    // 上传打开模态框
    handleKai() {
      this.dialogVisible = true;
      this.title = "上传对象";
    },
    // 上传成功回调
    handleAvatarSuccess(res, file) {
      console.log(res.errmsg, "123");
      this.imageUrl = URL.createObjectURL(file.raw);
      if (res.errmsg == "成功") {
        this.$notify({
          title: "成功",
          message: "上传成功",
          type: "success",
        });
        this.dialogVisible = false;
        this.handleAll();
      }
    },
    // 导出
    downLoad() {
      this.$confirm("此操作将导出excel表格, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 定义表头
          const headers = [
            "ID",
            "对象KEY",
            "对象名称",
            "对象类型",
            "对象大小",
            "访问链接",
          ]; // 将表格数据转换为工作表数据
          const worksheetData = [
            headers, // 第一行为表头
            ...this.tableData.map((item) => {
              return [
                item.id || "",
                item.key || "",
                item.name || "",
                item.type || "",
                item.size || "",
                item.url || "",
              ];
            }),
          ]; // 创建工作表

          const worksheet = XLSX.utils.aoa_to_sheet(worksheetData); // 创建工作簿

          const workbook = XLSX.utils.book_new();
          XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1"); // 导出 Excel 文件

          XLSX.writeFile(workbook, "对象存储信息.xlsx"); // 提示导出成功

          this.$message({
            type: "success",
            message: "导出成功！",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    // 删除
    handleDelete(row) {
      // console.log(row, "删除");
      postDelete({
        deleted: row.deleted,
        id: row.id,
        key: row.key,
        name: row.name,
        size: row.size,
        type: row.type,
        updateTime: row.updateTime,
        url: row.url,
      }).then((res) => {
        if (res.errmsg == "成功") {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
          this.handleAll();
        }
      });
    },
    // 编辑打开模态框并存储数据
    handleKaibian(row) {
      this.dialogVisible = true;
      this.title = "修改对象名称";
      this.bianrow = row;
    },
    // 编辑
    handleEdit() {
      postUpdate({
        deleted: this.bianrow.deleted,
        id: this.bianrow.id,
        key: this.bianrow.key,
        name: this.bianrow.name,
        size: this.bianrow.size,
        type: this.bianrow.type,
        updateTime: this.bianrow.updateTime,
        url: this.bianrow.url,
      }).then((res) => {
        console.log(res, "编辑成功");
        if (res.errmsg == "成功") {
          this.$notify({
            title: "成功",
            message: "更新成功",
            type: "success",
          }); // 刷新表格
          this.handleAll();
          this.dialogVisible = false;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.obj.limit = val;
      this.handleAll();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.obj.page = val;
      this.handleAll();
      console.log(`当前页: ${val}`);
    },
  },
  mounted() {
    this.handleAll();
  },
};
</script>

<style scoped lang="scss">
.box::v-deep {
  width: 98%;
  padding: 1%;
  .BoxTop {
    margin-top: 5px;
    width: 50%;
    display: flex;
  }
  .BoxMiddle {
    margin-top: 20px;
  }
  .BoxBottom {
    margin-top: 20px;
  }
}
</style>
